import time
import requests
from Common.connect_to_device import *
from Common.hande_device import HandleDevices
from influxdb_client import Point

from Common.handle_influxdb import InfluxDBHandler

do_devices = HandleDevices(CPDemo_pkgName=MID_pkgName, SDKServer_pkgName=SDKServer_pkgName)
do_influxdb = InfluxDBHandler()

apm_pid_main, apm_server_main = do_devices.handle_main_device(deviceId_001=device_1)

apm_pid_sec = do_devices.handle_sec_device(deviceId_002=device_2)

while True:
    # 准备数据点列表
    try:
        time.sleep(2)
        device_001_main_cpu = do_devices.get_cpu(apm_pid_main)["appCpuRate"]
        device_001_main_mem = do_devices.get_mem(apm_pid_main)
        device_001_server_cpu = do_devices.get_cpu(apm_server_main)["appCpuRate"]
        device_001_server_mem = do_devices.get_mem(apm_server_main)
        device_002_main_cpu = do_devices.get_cpu(apm_pid_sec)["appCpuRate"]
        device_002_main_mem = do_devices.get_mem(apm_pid_sec)

        measurements = [
            Point("device_001").tag("pid", "main_pid").field("cpu", device_001_main_cpu),
            Point("device_001").tag("pid", "main_pid").field("mem", device_001_main_mem),
            Point("device_001").tag("pid", "main_server").field("cpu", device_001_server_cpu),
            Point("device_001").tag("pid", "main_server").field("mem", device_001_server_mem),

            # 分割
            Point("device_002").tag("pid", "main_pid").field("cpu", device_002_main_cpu),
            Point("device_002").tag("pid", "main_pid").field("mem", device_002_main_mem)
        ]
        do_influxdb.write_mul_data(measurements=measurements)
    except:
        try:
            requests.get(url="https://api.day.app/s3KoLFVvWVWdYiiro2gKW/A1性能采集可能出现问题")
        except:
            pass

# 面板地址
# http://10.10.118.92:3000/d/83sIaa9Iz/dp_a1tai-jia?orgId=1&from=now-1h&to=now
